<template>
  <view class="commission-list-container">
    <!-- 顶部标题栏 -->
    <view class="header">
      <view class="back-btn" @click="goBack">
        <text class="back-arrow">←</text>
      </view>
      <text class="title">收益佣金列表</text>
    </view>
    
    <!-- 佣金列表 -->
    <view class="commission-list">
      <!-- 佣金项 -->
      <view class="commission-item" v-for="(item, index) in commissionRecords" :key="index">
      
        <view class="commission-details">
          <view class="commission-amount">
            <text class="amount-label">佣金金额</text>
            <text class="amount-value">+{{ item.commission }}</text>
          </view>
          <view class="commission-type">
            <text class="type-label">佣金类型</text>
            <text class="type-value">{{ item.productName }}</text>
          </view>
          <view class="commission-status" v-if="item.status !== undefined">
            <text class="status-label">状态</text>
            <text class="status-value">{{ getStatusName(item.status) }}</text>
          </view>
        </view>
      </view>
      
      <!-- 空状态 -->
      <view class="empty-state" v-if="commissionRecords.length === 0">
        <image src="/static/images/empty.png" mode="aspectFit" class="empty-image"></image>
        <text class="empty-text">暂无佣金记录</text>
      </view>
    </view>
  </view>
</template>

<script>
import { get } from '../../utils/request.js'

export default {
  data() {
    return {
      commissionRecords: [],
      loading: false,
      page: 1,
      pageSize: 10,
      hasMore: true
    }
  },
  
  onLoad() {
    // 页面加载时获取佣金列表
    this.getCommissionList()
  },
  
  methods: {
    // 返回上一页
    goBack() {
      uni.navigateBack()
    },
    
    // 获取佣金列表
    async getCommissionList() {
      if (this.loading || !this.hasMore) {
        return
      }
      
      this.loading = true
      try {
        const token = uni.getStorageSync('token')
        if (!token) {
          uni.showToast({
            title: '请先登录',
            icon: 'none'
          })
          return
        }
        
        // 调用API获取佣金列表
        const res = await get('/api/dify/commission/list', {
          page: this.page,
          pageSize: this.pageSize
        })
        
        console.log('佣金列表API返回结果:', res)
        
        if (res.code === 0 && res.data) {
          const dataList = res.data.list || []
          
          if (dataList.length < this.pageSize) {
            this.hasMore = false
          }
          
          // 如果是第一页，直接替换数据；否则追加
          if (this.page === 1) {
            this.commissionRecords = dataList
          } else {
            this.commissionRecords = [...this.commissionRecords, ...dataList]
          }
          
          this.page++
        } else {
          this.hasMore = false
        }
        
      } catch (error) {
        console.warn('API调用失败:', error)
        // 加载更多时失败，提示用户
        if (this.page > 1) {
          uni.showToast({
            title: '加载更多失败',
            icon: 'none'
          })
        }
      } finally {
        this.loading = false
      }
    },
    
    // 获取佣金类型名称
    getCommissionTypeName(type) {
      const typeMap = {
        1: '邀请佣金',
        2: '交易佣金',
        3: '活动佣金'
      }
      return typeMap[type] || '其他佣金'
    },
    
    // 获取状态名称
    getStatusName(status) {
      const statusMap = {
        0: '待结算',
        1: '已发放',
        2: '发放失败'
      }
      return statusMap[status] || '未知状态'
    },
    
    // 上拉加载更多
    onReachBottom() {
      this.getCommissionList()
    }
  }
}
</script>

<style scoped>
.commission-list-container {
  background-color: #3A3052;
  min-height: 100vh;
  padding-bottom: 120rpx;
}

.header {
  background-color: #4A4062;
  padding: 10px 65px 10px 15px;
  display: flex;
  align-items: center;
  position: sticky;
  top: 0;
  z-index: 100;
}

.back-btn {
  width: 40px;
  height: 40px;
  background-color: rgba(240, 217, 181, 0.1);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 10px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.back-btn:active {
  background-color: rgba(240, 217, 181, 0.2);
  transform: scale(0.95);
}

.back-arrow {
  color: #F0D9B5;
  font-size: 20px;
  font-weight: bold;
}

.title {
  flex: 1;
  color: rgba(255, 255, 255, 0.5);
  font-size: 16px;
  font-weight: 600;
  text-align: center;
}

.commission-list {
  padding: 0 20rpx 20rpx;
}

.commission-item {
  background-color: #4A4062;
  border-radius: 16rpx;
  padding: 25rpx;
  margin: 20rpx 0;
}

.commission-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 20rpx;
}

.user-info {
  display: flex;
  align-items: center;
}

.avatar {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  background-color: #F8E7D0;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 20rpx;
  overflow: hidden;
}

.avatar image {
  width: 100%;
  height: 100%;
}

.user-details {
  display: flex;
  flex-direction: column;
}

.username {
  color: #FFFFFF;
  font-size: 32rpx;
  font-weight: bold;
  margin-bottom: 5rpx;
}

.user-id {
  color: #8F82A8;
  font-size: 28rpx;
}

.commission-time {
  color: #8F82A8;
  font-size: 28rpx;
}

.commission-details {
  display: flex;
  flex-direction: column;
  gap: 15rpx;
}

.commission-amount,
.commission-type,
.commission-status {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.amount-label,
.type-label,
.status-label {
  color: #8F82A8;
  font-size: 28rpx;
}

.amount-value {
  color: #F0D9B5;
  font-size: 32rpx;
  font-weight: bold;
}

.type-value,
.status-value {
  color: #FFFFFF;
  font-size: 28rpx;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 0;
}

.empty-image {
  width: 200rpx;
  height: 200rpx;
  margin-bottom: 30rpx;
  opacity: 0.5;
}

.empty-text {
  color: #8F82A8;
  font-size: 32rpx;
}
</style>